lobsterlayui.main(['index', 'layer', 'form', 'element', 'table', 'laydate'], {
    data: {
        $layui: null,
        datatable: null,
    },
    pageload: function (right) {
        console.log(right);
        var self = this;
        // this.data.search.sdate = this.getStartTime();
        // this.data.search.edate = this.getEndTime();
        this.initDept(function () {
            self.loadData();
        });

        this.initevent();
    },
    initevent() {
        var self = this;
        //查找reload,通过条件，查询网格的table重载数据显示到界面
        layui.form.on("submit(LAY-app-front-search)", function (data) {
            self.loadData()
        });



        //监听行工具事件
        layui.table.on('tool(tabledata)', function (obj) {  //tool是工具条事件名，DeptInfoTable是table原始容器的属性
            var data = obj.data;//当前行数据
            var layEvent = obj.event;//获得lay-event的值
            switch (obj.event) {
                case 'disable':
                    var confirm_msg = "确认停用么?";
                    var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                    layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                        function (index) {
                            self.stopdata(data, 1, index);
                        });
                    break;
                case 'enable':
                    var confirm_msg = "确认启用么?";
                    var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                    layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                        function (index) {
                            self.stopdata(data, 0, index);
                        });
                    break;
                case 'schedual_down': //线下出诊
                    self.schedual(0, data);
                    break;
                case 'schedual_up': //线上出诊
                    self.schedual(1, data);
                    break;
                case 'setprice': //设置出诊费用
                    self.setprice(data);
                    break;
            }
        });
    },
    initDept(func) {
        var self = this;
        lobsterlayui.GETAPI('tsschedual.getselectdept').then(retdata => {
            var depts = retdata.selectdata || [];
            var options = "<option value='0'>全部</option>";
            depts.forEach(dept => {
                options += "<option value='" + dept.DeptId + "'>" + dept.Name + "</option>";
            })
            $("#cmbDept").html(options);
            layui.form.render("select");

            func && func();
        })
    },
    loadData() {
        var self = this;

        self.data.datatable = layui.table.render({
            elem: '#tabledata',
            where: {
                deptId: $("#cmbDept").val(),
                keyword: $("#txtkeyword").val(),
                status: $("#status").val(),
            },
            headers: lobsterlayui.GetHeaderData(),
            url: lobsterlayui.GetLowCodeUrl('tsschedual.gettabledoctor'),
            height: 'full-130',
            cellMinWidth: 80,
            size: 'sm',
            page: true,
            even: true,
            cols: [[
                { field: "EmpId", width: "80", title: "编号" },
                { field: "EmpName", width: "200", title: "姓名" },
                { field: "", width: "100", title: "性别", templet: "#barSex" },
                { field: "DoctorPost", width: "120", title: "职称" },
                { field: "", width: "100", title: "头像", templet: "#barRowImg" },
                { field: "Telephone", width: "100", title: "电话" },
                { field: "Describe", width: "300", title: "医生简介" },
                { field: "RegPrice", width: "100", title: "挂号费" },
                { field: "IHPrice", width: "100", title: "问诊费" },

                { title: "操作", width: "300", fixed: "right", toolbar: "#barRowDoctor" }
            ]],
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.data
                }
            }
        });
    },
    schedual(type, row) {
        var self = this;
        layer.open({
            type: 1,
            title: '排班数据-' + (type == 0 ? '线下' : '线上'),
            content: $("#dlg_generateschedual").html(),
            area: ['500px', '70%'],
            success: function (layero, index) {
                layui.laydate.render({
                    elem: "#schedualdate",
                    //type: "datetime",
                    format: 'yyyy-MM-dd',
                    type: "date",
                    //format: 'yyyy-MM-dd',
                    value: self.getStartTime() + ' ~ ' + self.getEndTime(),
                    range: '~',
                    change: function (value, date, endDate) {
                        $("#schedualdate").val(value);
                    },
                    done: function (value, date, endDate) {
                        $("#schedualdate").val(value);
                    }
                });

                //查询排班
                layui.form.on("submit(btnSearchSchedual)", function (data) {
                    var doctId = row.EmpId;
                    var dstr = $("#schedualdate").val();
                    var sdate = dstr.split(" ~ ")[0];
                    var edate = dstr.split(" ~ ")[1];
                    self.sched_loadtable(type, doctId, sdate, edate);
                });
                //触发查询
                $('#btnSearchSchedual').trigger('click');

                //生成排班界面
                layui.table.on("toolbar(tableschedual)", function (obj) {
                    if (obj.event == "generate") {
                        layer.open({
                            type: 1,
                            title: '生成排班-' + (type == 0 ? '线下' : '线上'),
                            content: $("#dlg_generatefrom").html(),
                            area: ['500px', '70%'],
                            btn: ['提交', '取消'],
                            success: function (layero, index) {
                                layui.form.val('formData', { doctId: row.EmpId, deptId: row.DeptId, SchedType: type });

                                layui.laydate.render({
                                    elem: "#schedualdate_from",
                                    //type: "datetime",
                                    format: 'yyyy-MM-dd',
                                    type: "date",
                                    //format: 'yyyy-MM-dd',
                                    value: self.getStartTime() + ' ~ ' + self.getEndTime(),
                                    range: '~',
                                    change: function (value, date, endDate) {
                                        $("#schedualdate_from").val(value);
                                    },
                                    done: function (value, date, endDate) {
                                        $("#schedualdate_from").val(value);
                                    }
                                });
                            },
                            yes: (index, layero) => {
                                //提交
                                var submit = $('#submit_generate');
                                //监听提交
                                layui.form.on('submit(submit_generate)', function (data) {
                                    var field = data.field; //获取提交的字段
                                    self.sched_generate(field, function () {
                                        layer.close(index);
                                        //触发查询
                                        $('#btnSearchSchedual').trigger('click');
                                    });
                                });
                                submit.trigger('click');
                            }
                        });
                    }
                });

                //监听行工具事件
                layui.table.on('tool(tableschedual)', function (obj) {
                    var data = obj.data;//当前行数据
                    var layEvent = obj.event;//获得lay-event的值
                    switch (obj.event) {
                        case 'disable':
                            var confirm_msg = "确认停用么?";
                            var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                            layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                                function (index) {
                                    self.sched_stopdata(data.SchedualID, 1, function () {
                                        layer.close(index);
                                        //触发查询
                                        $('#btnSearchSchedual').trigger('click');
                                    });
                                });
                            break;
                        case 'enable':
                            var confirm_msg = "确认启用么?";
                            var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                            layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                                function (index) {
                                    self.sched_stopdata(data.SchedualID, 0, function () {
                                        layer.close(index);
                                        //触发查询
                                        $('#btnSearchSchedual').trigger('click');
                                    });
                                });
                            break;
                    }
                });
            },
            yes: (index, layero) => {

            }
        });
    },
    setprice(row) {
        var self = this;
        layer.open({
            type: 1,
            title: '配置诊查费',
            content: $("#dlg_setpricefrom").html(),
            // offset: ['10%', '53%'],
            area: ['400px', '300px'],
            btn: ['确定', '取消'],
            success: function (layero, index) {
                layui.form.val('formData2', { Id: row.Id });
            },
            yes: (index, layero) => {
                //提交
                var submit = $('#submit_setprice');
                //监听提交
                layui.form.on('submit(submit_setprice)', function (data) {
                    var field = data.field; //获取提交的字段
                    lobsterlayui.POSTAPI('tsschedual.setdoctorprice',field).then(retdata=>{
                        layer.close(index);
                        self.data.datatable.reload();
                    })
                });
                submit.trigger('click');
            }
        });
    },
    getEndTime: function () {
        var dateTime = new Date();
        var mdate = new Date(dateTime.setDate(dateTime.getDate() + 7));
        var year = mdate.getFullYear();
        var month = mdate.getMonth() + 1;
        if (month < 10)
            month = "0" + month;
        var ddate = mdate.getDate();
        if (ddate < 10)
            ddate = "0" + ddate;
        return year + "-" + month + "-" + ddate;
    },
    getStartTime: function () {
        var mdate = new Date();
        var year = mdate.getFullYear();
        var month = mdate.getMonth() + 1;
        if (month < 10)
            month = "0" + month;
        var ddate = mdate.getDate();
        if (ddate < 10)
            ddate = "0" + ddate;
        return year + "-" + month + "-" + ddate;
    },
    sched_loadtable(SchedType, doctId, sdate, edate) {
        layui.table.render({
            elem: '#tableschedual',
            where: {
                SchedType: SchedType,
                doctId: doctId,
                sdate: sdate,
                edate: edate,
            },
            headers: lobsterlayui.GetHeaderData(),
            url: lobsterlayui.GetLowCodeUrl('tsschedual.gettableschedual'),
            height: 'auto',
            cellMinWidth: 80,
            toolbar: '#tool_generateschedual',
            size: 'sm',
            page: true,
            even: true,
            cols: [[
                { field: "SchedualDate", width: "150", title: "日期" },
                { field: "TimeRange", width: "100", title: "班次", templet: "#text_TimeRange" },
                { field: "AppointNum", width: "100", title: "号源", },
                { title: "操作", width: "80", fixed: "right", toolbar: "#barSchedual" }
            ]],
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.data
                }
            }
        });
    },
    sched_stopdata(schedId, flag, func) {
        lobsterlayui.GETAPI('tsschedual.stopschedual', { schedId: schedId, flag: flag }).then(retdata => {
            layer.msg('操作成功');
            func && func();
        })
    },
    sched_generate(field, func) {
        var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
        var sdate = field.schedualdate_from.split(" ~ ")[0];
        var edate = field.schedualdate_from.split(" ~ ")[1];
        var data = {
            doctId: field.doctId,
            deptId: field.deptId,
            SchedType: field.SchedType,
            sdate: sdate,
            edate: edate,
            am_num: field.am_num,
            pm_num: field.pm_num,
            nm_num: field.nm_num
        }
        console.log(data);

        lobsterlayui.POSTAPI('tsschedual.generateschedual', data).then(retdata => {
            layer.close(load);

            layer.msg('生成排班成功');

            func && func();
        })


    }
})